package denisLunchy;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

public class Menu extends LunchyClass {
	public ArrayList<Dish> arrayDish = new ArrayList<Dish>();
	public ArrayList<Order> arrayOrder = new ArrayList<>();
	public ArrayList<Client> arrayClient = new ArrayList<>();

	@Override
	public void addItem(Dish dish) {
		Dish updDish;
		boolean updt = false;
		int i = 0;
		// TODO Auto-generated method stub
		dish.setStatus(false);
		for (Dish d : arrayDish) {

			if (dish.getName().equals(d.getName())) {
				updt = true;
				break;
			} else {
				i++;
			}
		}
		if (!updt) {
			arrayDish.add(dish);
		} else if (updt) {
			updDish = arrayDish.get(i);
			updDish.setId(dish.setId());
			updDish.setType(dish.getType());
			updDish.setCost(dish.getCost());
			updDish.setCount(dish.getCount());
			updDish.setStatus(dish.getStatus());
		}
	}

	@Override
	public void removeItem(Dish dish) {
		// TODO Auto-generated method stub
		dish.setStatus(true);
	}

	public Dish createDish(String name, int cost, int count, String typeDish) {
		Dish dish = new Dish();
		dish.setName(name);
		dish.setCost(cost);
		dish.setCount(count);
		dish.setType(typeDish);
		dish.setStatus(false);
		return dish;

	}

	public void addOrder(Order order) {
		boolean updt = false;
		int i = 0;
		for (Order ordered : arrayOrder) {
			if (ordered.getClient().getSurName()
					.equals(order.getClient().getSurName())) {
				updt = true;
				break;
			} else {
				i++;
			}
		}
		if (!updt) {
			arrayOrder.add(order);
		//	arrayClient.add(order.getClient());
		} else {
			arrayOrder.remove(i);
			arrayOrder.add(i, order);
		}
	}

	public void RemoceOrder(Order order) {
		arrayOrder.remove(order);
	}

	public void AddClient(Client client) {
		arrayClient.add(client);
	}

	public void SaveClList() {
		PrintStream printStream;
		try {
			printStream = new PrintStream(new FileOutputStream(
					"ListClient.txt", true), true);
			for (Client client : arrayClient) {
				printStream.println(client.getNameClient() + ";"
						+ client.getSurName());
			}
			printStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void loadListCl() {
		String line;
		String[] List = null;
		Client client;
		try {
			FileReader fileReader = new FileReader("ListClient.txt");
			BufferedReader reader = new BufferedReader(fileReader);
			while ((line = reader.readLine()) != null) {
				client = new Client();
				List = line.split(";");
				client.setNameClient(List[0]);
				client.setSurName(List[1]);
				AddClient(client);
			}
			fileReader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public Order findOrder(String name,String srName){
		for(Order order:arrayOrder){
			if (order.getClient().getNameClient().equals(name)){
				if(order.getClient().getSurName().equals(srName)){
					return order;
				}
			}
		}
		return null;
		
	}
	public Dish findDish(String name,ArrayList<Dish> list){
		for(Dish dish: list){
			if(dish.getName().equals(name)){
				return dish;
			}
		}
		return null;
		
	}
	}

